package advanced.datastructure;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import structure5.Association;

public class StudentInfoGen {

	public static void main(String args[]) throws FileNotFoundException
	{
        //creating File instance to reference text file in Java
        File text = new File("src/advanced/datastructure/pbook2.txt");
      
        //Creating Scanner instnace to read File in Java
        Scanner scnr = new Scanner(text);
      
        //Reading each line of file using Scanner class
        MyVector<Student> students = new MyVector<Student>();
     
        String name="";
        String address="";
        long campusPhone=-1;
        int suBox=-1;
        long homePhone=-1;
        String thirdLine;
        String contactNums[];
        Student stu = new Student();
        AddressStudents addrStuAssoc = new AddressStudents();
        AreaCodeCounter areaCodeCounter = new AreaCodeCounter();
        String line;
        while(scnr.hasNextLine()){
            line = scnr.nextLine();
            
            if(!line.equals("------------")){
            	name=line;
            	address=scnr.nextLine();
            	thirdLine=scnr.nextLine();
            	contactNums=thirdLine.split("\\s+");
            	if(contactNums.length==3)
            	{
            		campusPhone=getPhoneNum(contactNums[0]);
            		suBox=getSuBox(contactNums[1]);
            		homePhone=getPhoneNum(contactNums[2]);
            		areaCodeCounter.updateCount(getAreaCode(contactNums[2]));
            	}
            	stu = new Student();
            	stu.setName(name);
            	stu.setFullAddress(address);
            	stu.setCampusPhoneNum(campusPhone);
            	stu.setSuBox(suBox);
            	stu.setHomePhoneNum(homePhone);
            	students.add(stu);
            	addrStuAssoc.updateCount(stu.getAddress());
            }
        }
        
        firstStudentInPhoneBook(students);
        //print(students.getFirst().getName());
        print("*********************************************************************************************************");
      
        smallestAndLargestSuBox(students);
         //print(""+students.getFirst().getSuBox());
        //print(""+students.getLast().getSuBox());
        print("*********************************************************************************************************");
        
        mostVowelsName(students);
        print(students.getLast().getName());
        print("*********************************************************************************************************");
        
        
        addressSharedByMostStudents(addrStuAssoc);
        //print(addrStuAssoc.addressStudentAssoc.toString());
        print("*********************************************************************************************************");
        
        areaCodeSharedByMostStudents(areaCodeCounter);
        //System.out.println(areaCodeCounter.areaCodeCounts);
        print("*********************************************************************************************************");
	}
	
	public static void firstStudentInPhoneBook(MyVector<Student> students)
	{
		students.sort(new StudentNameComparator());
		String name = students.getFirst ().getName();
		print(name+" appears first in a printed phone book.");
	}
	
	public static void smallestAndLargestSuBox(MyVector<Student> students)
	{
		students.sort(new StudentSuBoxComparator());
		Student stu1=students.getFirst();	
		for(int i=0; i<students.size(); i++)
		{
			if(students.get(i).getSuBox()>-1)
			{
				stu1=students.get(i);
				break;
			}
		}
		
		Student stu2=students.getLast();	//student with the largest su box number
		
		print(stu1.getName()+" has the smallest su box number of "+stu1.getSuBox());
		print(stu2.getName()+" has the largest su box number of "+stu2.getSuBox());
	}
	
	public static void mostVowelsName(MyVector<Student> students)
	{
		 students.sort(new StudentVowelComparator());
		 Student stu = students.getLast();
		 
		 print(stu.getName()+" has the most vowels in the name.");
	}
	
	public static void addressSharedByMostStudents(AddressStudents addrStuAssoc)
	{
		Association<String, Integer> addAssoc = addrStuAssoc.getAddressSharedByMost();
		String address = addAssoc.getKey();
		Integer numOfStus = addAssoc.getValue();
		print(address+" has "+ numOfStus+" students, it is shared by most students.");
	}
	
	public static void areaCodeSharedByMostStudents(AreaCodeCounter areaCodeCounter)
	{
		Association<String, Integer> areaCodeAssoc=areaCodeCounter.getAreaCodeSharedByMost();
		String areaCode=(String)areaCodeAssoc.getKey();
		Integer numOfStus = areaCodeAssoc.getValue();
		print("The area code "+areaCode+" has "+numOfStus+" students, it is shared by most students.");
	}
	
	public static void print(String s)
	{
		System.out.println(s);
	}
	public static void printStudents(MyVector<Student> stus)
	{
		for(int i=0; i<stus.size(); i++)
		{
			System.out.println(stus.get(i));
		}
	}
	
	public static String getAreaCode(String ph)
	{
		if(ph.equals("-1"))
		{
			return ph;
		}
		else if(ph.trim().length()==10)
		{
			return ph.substring(0,3);
		}
		return ph;
	}
	public static long getPhoneNum(String ph)
	{
		long phNum=-1;
		if(isNumeric(ph))
		{
			phNum=Long.parseLong(ph);
		}
		return phNum;
	}
	
	public static int getSuBox(String su)
	{
		int suBox=-1;
		if(isNumeric(su))
		{
			suBox=Integer.parseInt(su);
		}
		return suBox;
	}
	
	public static boolean isNumeric(String str)  
	{  
	  try  
	  {  
	    double d = Double.parseDouble(str);  
	  }  
	  catch(NumberFormatException nfe)  
	  {  
	    return false;  
	  }  
	  return true;  
	}
	
}
